﻿namespace Tetris.Game.Common.TetrisCenter.TetrisBase
{
    public class TetrisGameResult : ICloneable
    {
        private readonly IFunction _scoreLevel;
        private readonly IFunction _levelInterval;
        public TetrisGameResult()
        {
            Score = 0;
            Level = 1;
            Interval = 1000;
            ScoreThreshold = 50;

            _scoreLevel = new ScoreLevel(0.02f, 1);
            _levelInterval = new LevelInterval(1110, -0.1f);
        }
        public long Score { get; private set; }
        public int RowNumber { get; private set; }
        public int Level { get; set; }
        public int Interval { get; set; }
        public long ScoreThreshold { get; private set; }
        public void AddRowNumber(int row)
        {
            if (row < 1) return;
            RowNumber += row;
            Score += (row switch
            {
                1 => 1,
                2 => 3,
                3 => 6,
                _ => 10
            }) * Level;

            // 得分与关卡
            Level = _scoreLevel.Function(Score);
            // 关卡与间隔
            Interval= _levelInterval.Function(Level);
        }

        public object Clone()
        {
            return this.MemberwiseClone();
        }
    }
    class ScoreLevel : IFunction
    {
        public ScoreLevel(float a,float c)
        {
            A = a;
            C = c;
        }
        public float A { get; set; }
        public float C { get; set; }
        public int Function(long x)
        {
            float result = A * x + C;
            return (int)result;
        }
    }
    class LevelInterval : IFunction
    {
        public LevelInterval(float a, float b)
        {
            A = a;
            B = b;
        }
        public float A { get; set; }
        public float B { get; set; }

        public int Function(long x)
        {
            double result = A * Math.Pow(Math.E, B * x);
            return (int)result;
        }
    }
    interface IFunction
    {
        int Function(long x);
    }
}
